﻿using System.Xml.Linq;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Xml;

class Program
{
    static void Main()
    {

        string xmlFilePath = "if.vsc";
        XmlDocument xml = new XmlDocument();
        xml.Load(xmlFilePath);
        string jsonString = JsonConvert.SerializeXmlNode(xml);
        File.WriteAllText("if.json", JsonConvert.SerializeObject(jsonString, Newtonsoft.Json.Formatting.Indented));
        //string xmlData = File.ReadAllText("if.vsc");
        //var doc = XDocument.Parse(xmlData);
        //var functionElement = doc.Descendants("Function").FirstOrDefault();

        //if (functionElement != null)
        //{
        //    var functionName = functionElement.Element("Name")?.Attribute("value")?.Value;
        //    var bodyElement = functionElement.Descendants("Body").FirstOrDefault();
        //    var statements = bodyElement.Descendants("Statement").Select(statement =>
        //    {
        //        var expressionElement = statement.Element("Expression");
        //        var innerExpression = expressionElement.Element("Expression");

        //        var jsonExpression = new JObject
        //        {
        //            ["Type"] = innerExpression.Attribute("type").Value.Split(',').First(),
        //        };

        //        foreach (var child in innerExpression.Elements())
        //        {
        //            jsonExpression[child.Name.LocalName] = child.Attribute("value")?.Value;
        //        }

        //        return new JObject
        //        {
        //            ["Type"] = expressionElement.Attribute("type").Value.Split(',').First(),
        //            ["Expression"] = jsonExpression
        //        };
        //    }).ToArray();

        //    var result = new JObject
        //    {
        //        ["Function"] = new JObject
        //        {
        //            ["Name"] = functionName,
        //            ["Body"] = new JArray(statements)
        //        }
        //    };

        //    File.WriteAllText("if.json", JsonConvert.SerializeObject(result, Newtonsoft.Json.Formatting.Indented));
    //}
    }
}
